 package SortSearch;

import java.util.ArrayList;

import DataModel.Feature;
import DataModel.FeatureBucket;
import DataModel.FeatureHolder;


 /**
  * This class implements the binary search for sortable elements
 * @author Stefan Lorenz<br>
 * <br>26.11.2012
 */
public class BinarySearch{
     
     //returns the index of a sortable element in a list
     public  int binarySearchRead(FeatureHolder arrayList,int x){
         int low = 0;
         int high = arrayList.size() - 1;
         int mid = 0;
         while( low <= high ){
             
             mid = ( low + high ) / 2;

             if(arrayList.get(0).getStart() > x )
        	 return 0;
             if( arrayList.get(mid).getStart() < x && arrayList.get(mid).getStop() < x)
                 low = mid + 1;
             else if(  arrayList.get(mid).getStart() > x && arrayList.get(mid).getStop() > x)
                 high = mid - 1;
             else
                 return mid;
         }
         //return last element if the searched value is bigger than last element
         if(arrayList.get(arrayList.size()-1).getStart() < x)
             return arrayList.size()-1;

         return mid;     // not found return last tested index
     }
     
     public  int binarySearchFeatures(ArrayList<Feature> arrayList,int x){
         int low = 0;
         int high = arrayList.size() - 1;
         int mid = 0;
         while( low <= high ){
             
             mid = ( low + high ) / 2;

             if(arrayList.get(0).getStart() > x )
        	 return 0;
             if( arrayList.get(mid).getStart() < x && arrayList.get(mid).getStop() < x)
                 low = mid + 1;
             else if(  arrayList.get(mid).getStart() > x && arrayList.get(mid).getStop() > x)
                 high = mid - 1;
             else
                 return mid;
         }
         //return last element if the searched value is bigger than last element
         if(arrayList.get(arrayList.size()-1).getStart() < x)
             return arrayList.size()-1;

         return mid;     // not dounf return last tested index
     }

     public  int binarySearchTranscriptionalActiveRegion(ArrayList<FeatureBucket> arrayList,int x){
         int low = 0;
         int high = arrayList.size() - 1;
         int mid = 0;
         while( low <= high ){
             
             mid = ( low + high ) / 2;

             if(arrayList.get(0).getStart() > x ){
            	 return 0;
             }
             if( arrayList.get(mid).getStart() < x && arrayList.get(mid).getStop() < x){
                 low = mid + 1;
             }
             else if(  arrayList.get(mid).getStart() > x && arrayList.get(mid).getStop() > x){
                 high = mid - 1;
             }
             else{
                 return mid;
             }
         }
         //return last element if the searched value is bigger than last element
//         if(arrayList.get(arrayList.size()-1).getStart() < x)
//             return arrayList.size()-1;
         
         return mid;     // not dounf return last tested index
     }
 }